# encoding:utf-8
from config.all_paths import get_root_path, config_variable_path
from utils.others_tool.other_tool import list_dir
import os

base_dir = get_root_path()


def read_big_txt_1(filename):
    # 读取大txt文件的最后一行
    try:
        filesize = os.path.getsize(filename)
        if filesize == 0:
            return None
        else:
            with open(filename, 'rb') as fp:
                offset = -8
                while -offset < filesize:
                    fp.seek(offset, 2)
                    lines = fp.readlines()
                    if len(lines) >= 2:
                        print(str(lines[-1]), 'utf-8')
                        return lines[-1]
                    else:
                        offset *= 2
                fp.seek(0)
                lines = fp.readlines()
                print(str(lines[-1]), 'utf-8')
                return lines[-1]
    except FileNotFoundError:
        print(filename + ' not found!')
        return None


def read_big_txt(text_file):
    with open(text_file, 'r', encoding='utf-8') as f:  # 打开文件
        lines = f.readlines()  # 读取所有行
        last_line = lines[-1]  # 取最后一行
        return last_line


# 读取excel倒数第二行
def read_big_txt_2(text_file):
    with open(text_file, 'r', encoding='utf-8') as f:  # 打开文件
        lines = f.readlines()  # 读取所有行
        last_line = lines[-2]  # 取最后一行
        return last_line


def read_txt_lines(text_file):
    # 逐行读取
    with open(text_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        return lines


def read_txt(text_file):
    """读取txt文件"""
    with open(text_file, 'r', encoding='utf-8') as f:
        return f.read()


def write_txt(text_file, data):
    """追加写入txt文件"""
    with open(text_file, 'a', encoding='utf-8') as f:
        f.write(data)
        f.write('\n')  # 换行


def extract_txt(text_file, data):
    """覆盖写入txt文件"""
    with open(text_file, 'w+', encoding='utf-8') as f:
        f.write(data)


def truncate_txt(text_file_path):
    """清空txt文件"""
    text_files = list_dir(text_file_path, fileName_list=['__init__.py','.gitkeep'])
    for text_file in text_files:
        with open(text_file, 'w') as f:
            f.truncate()

def delete_txt(text_file_path):
    '''删除txt文件'''
    text_files = list_dir(text_file_path, fileName_list=['.gitkeep'])
    for text_file in text_files:
        os.remove(text_file)


# 快速判断当前文件夹下面的txt文件为空
def txtIsEmpty(text_file_path):
    text_files = list_dir(text_file_path, fileName_list=['.gitkeep'])
    for text_file in text_files:
        size = os.path.getsize(text_file)
        if size == 0:
            text_files.remove(text_file)
        else:
            pass
    for text_file in text_files:
        value = read_txt(text_file)
        if value == '' or value is None:
            text_files.remove(text_file)
        else:
            pass
    return text_files


def read_txt_handel(text_file):
    """将保存运行结果的txt数据，处理成想要的格式"""
    value = read_txt_lines(text_file)
    temlist = []
    for i in value:
        temdick = {}
        i_list = i[0:-1].split('|')
        temdick['id'] = i_list[0]
        temdick['excute_response'] = i_list[-1]
        temdick['excute_result'] = i_list[1]
        temlist.append(temdick)

    return temlist


if __name__ == '__main__':
    print(read_txt(config_variable_path))